﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using Winthusiasm.HtmlEditor;

namespace Phong_CTSV
{
    public partial class TextEditor : System.Web.UI.Page
    {
        // Variable Declaration
        QuerrySQL querry = new QuerrySQL();        

        protected bool InternetExplorer
        {
            get { return Request.Browser.Browser.Equals("IE"); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["userName"] != null)
                {
                    string userName = Session["userName"].ToString();
                    if (querry.Select_RoleID(userName) != 1)
                    {
                        Response.Redirect("~/LoginPage.aspx");
                    }                   
                }
                if (Session["userName"] == null)
                {
                    Response.Redirect("~/LoginPage.aspx");
                }
            }
            catch (Exception)
            {
                Response.Redirect("~/LoginPage.aspx");                
            }
            if (!IsPostBack)
            {
                Editor.Text = "";
                try
                {
                    var news = querry.Select_LastNews();
                    txtTitle.Text = news.Title;
                    Editor.Text = news._Content;
                }
                catch (Exception)
                {
                    txtTitle.Text = " ";
                    Editor.Text = " ";
                }
            }           
        }

        protected override void OnInitComplete(EventArgs e)
        {
            base.OnInitComplete(e);

            if (!IsPostBack)
            {
                string toggleMode = this.Request.QueryString["ToggleMode"];
                if (toggleMode != null)
                    Editor.ToggleMode = GetToggleMode(toggleMode);

                string colorScheme = this.Request.QueryString["ColorScheme"];
                if (colorScheme != null)
                    Editor.ColorScheme = GetColorScheme(colorScheme);

                string noToolstripBackgroundImage = this.Request.QueryString["NoToolstripBackgroundImage"];
                if (noToolstripBackgroundImage != null)
                    Editor.NoToolstripBackgroundImage = noToolstripBackgroundImage == "true";

                string xhtml = this.Request.QueryString["XHTML"];
                if (xhtml != null)
                    Editor.OutputXHTML = xhtml == "true";

                string deprecated = this.Request.QueryString["Deprecated"];
                if (deprecated != null)
                    Editor.ConvertDeprecatedSyntax = deprecated == "true";

                string paragraphs = this.Request.QueryString["Paragraphs"];
                if (paragraphs != null)
                    Editor.ConvertParagraphs = paragraphs == "true";
            }
        }        

        protected HtmlEditor.ToggleModeType GetToggleMode(string toggleMode)
        {
            HtmlEditor.ToggleModeType toggleModeType;

            switch (toggleMode)
            {
                case "Tabs":
                    toggleModeType = HtmlEditor.ToggleModeType.Tabs;
                    break;
                case "ToggleButton":
                    toggleModeType = HtmlEditor.ToggleModeType.ToggleButton;
                    break;
                case "Buttons":
                    toggleModeType = HtmlEditor.ToggleModeType.Buttons;
                    break;
                case "None":
                    toggleModeType = HtmlEditor.ToggleModeType.None;
                    break;
                default:
                    toggleModeType = HtmlEditor.ToggleModeType.Tabs;
                    break;
            }

            return toggleModeType;
        }

        protected HtmlEditor.ColorSchemeType GetColorScheme(string colorScheme)
        {
            HtmlEditor.ColorSchemeType colorSchemeType;

            switch (colorScheme)
            {
                case "Custom":
                    colorSchemeType = HtmlEditor.ColorSchemeType.Custom;
                    break;
                case "VisualStudio":
                    colorSchemeType = HtmlEditor.ColorSchemeType.VisualStudio;
                    break;
                default:
                    colorSchemeType = HtmlEditor.ColorSchemeType.Default;
                    break;
            }

            return colorSchemeType;
        }

        protected void ClearButton_Click(object sender, EventArgs e)
        {
            Editor.Text = String.Empty;
        }

        protected void XHTMLBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox box = (CheckBox)sender;
            Editor.OutputXHTML = box.Checked;          

            Editor.Revert();
            UpdatePanel1.Update();
        }

        protected void DeprecatedBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox box = (CheckBox)sender;
            Editor.ConvertDeprecatedSyntax = box.Checked;

            Editor.Revert();
            UpdatePanel1.Update();
        }

        protected void ParagraphsBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox box = (CheckBox)sender;
            Editor.ConvertParagraphs = box.Checked;

            Editor.Revert();
            UpdatePanel1.Update();
        }
        // Show messagebox
        private void MessageBox(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + message + "');", true);
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Editor.Text == null)
            {
                MessageBox("Bạn chưa nhập nội dung cho trang chủ. Vui lòng chọn OK sau đó nhập nội dung.");
            }
            else
            {
                string title = txtTitle.Text;
                InsertData(title, Editor.Text);
                Response.Redirect("~/Default.aspx");
            }            
        }

        private void InsertData(string title, string content)
        {
            querry.Update_News(1, content, title, DateTime.Now);
        }

        public string getTextEditor()
        {
            return Editor.Text;
        }

        protected string GetRedirectUrl()
        {
            string url = "~/TextEditor.aspx";      
            return url;
        }

        protected void Redirect_EventHandler(object sender, EventArgs e)
        {
            this.Response.Redirect(GetRedirectUrl());
        }

      
    }
}